'''
Created on Jan 2013

@author: dexen
'''

class Grid():

    pattern_root  = [ 0,0,0,
                      0,0,0,
                      0,0,0  ]
    pattern_right = [ 0,0,0,
                      1,0,0,
                      0,0,0  ]
    pattern_left = [  0,0,0,
                      0,0,1,
                      0,0,0  ]
    pattern_up =   [  0,0,0,
                      0,0,0,
                      0,1,0  ]
    pattern_down = [  0,1,0,
                      0,0,0,
                      0,0,0  ]
    patterns_growth = [pattern_right, pattern_left, pattern_up, pattern_down]
    
    def __init__(self, num_cols, num_rows): 
        self.num_cols = num_cols    
        self.num_rows = num_rows
        self.data = [[],[],[],[],[],[],[],[],[],[]]
        for row in range(self.num_rows):
            for col in range(self.num_cols):
                self.data[row].append(0)
    def get_point(self, point):
        col = point[0]
        row = point[1]    
        if row >= self.num_rows or col >= self.num_cols:
            return None
        return self.data[row][col]
    def set_point_on(self, point):
        col = point[0]
        row = point[1] 
        if row >= self.num_rows or col >= self.num_cols:
            return
        self.data[row][col] = 1
    def set_point_off(self, point):
        col = point[0]
        row = point[1] 
        if row >= self.num_rows or col >= self.num_cols:
            return
        self.data[row][col] = 0        
    def get_neighbours(self,point):
        col = point[0]
        row = point[1]   
        return [
            self.get_point((col-1, row+1)),
            self.get_point((col, row+1)),
            self.get_point((col+1, row+1)),
            
            self.get_point((col-1, row)),
            self.get_point((col, row)),
            self.get_point((col+1, row)),
            
            self.get_point((col-1, row-1)),
            self.get_point((col, row-1)),
            self.get_point((col+1, row-1))
        ]
    def match_pattern(self, point, pattern):
        neighbours = self.get_neighbours(point)
        for i in range(9):
            if neighbours[i] != pattern[i]:
                return false
        return true
    def match_paterns(self, point, patterns):
        for pattern in patterns:
            if not self.match_pattern(point, pattern):
                return false
        return true
    def get_growth_points(self):
        growth_points = []
        for row in range(self.num_rows):
            for col in range(self.num_cols):
                if self.match_patterns((col, row), self.patterns_growth):
                    growth_points.append((col, row))
        return growth_points
    def get_root_points():
        root_points = []
        for row in range(self.num_rows):
            for col in range(self.num_cols):
                if self.match_pattern((col, row), self.pattern_root):
                    root_points.append((col, row))
        return root_points
    def __repr__(self):
        str_rows = []
        for row in range(self.num_rows):
            str_rows.append("[" + ",".join([str(i) for i in self.data[row]]) + "]\n")
        str_repr = "[" + ",".join(str_rows) + "]"
        return str_repr
        
#TESTING
if __name__ == "__main__":
    grid = Grid(8,10)
    grid.set_point_on((7,0))
    grid.set_point_on((4,5))
    grid.set_point_on((5,5))
    grid.set_point_off((5,5))
    assert grid.get_point((4,5)) == 1
    print grid
            
    print grid.get_neighbours((4,5))
            
            
